package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.activity.GameDetailActivity;
import com.bocai.yuweiwangluo.bean.GameListBean;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.utils.UrlData;


public class GameListAdapter extends BaseAdapter
{
    private Context context;
    private GameListBean gameListBean;
    public GameListAdapter(Context context,GameListBean gameListBean)
    {
        this.context=context;
        this.gameListBean=gameListBean;
    }
    public void addData(GameListBean bean){
        gameListBean.getData().addAll(bean.getData());
        notifyDataSetChanged();
    }
    @Override
    public int getCount()
    {
        return gameListBean.getData().size();
    }

    @Override
    public Object getItem(int position)
    {
        return gameListBean.getData().get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_game_list,null);
            viewHolder.btn_game=(TextView)convertView.findViewById(R.id.btn_game);
            viewHolder.ivGameImg=(ImageView)convertView.findViewById(R.id.iv_game_img);
            viewHolder.tvGameName=(TextView)convertView.findViewById(R.id.tv_game_gamename);
            viewHolder.tvDistance=(TextView)convertView.findViewById(R.id.tv_game_distance);
            viewHolder.tvTime=(TextView)convertView.findViewById(R.id.tv_game_time);
            viewHolder.tvAddress=(TextView)convertView.findViewById(R.id.tv_game_address);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        GameListBean.Data data=gameListBean.getData().get(position);
        ImageLoaderUtil.loaderImageDefault(UrlData.IMAGEURL + data.getPic(), viewHolder.ivGameImg);
        viewHolder.tvGameName.setText(data.getTitle());
        viewHolder.tvTime.setText(TimeUtils.getTime(Long.parseLong(data.getGame_time())));
        viewHolder.tvAddress.setText(data.getAddr());
        //viewHolder.btn_game.setOnClickListener(new MyOnClickListner(position));
        return convertView;
    }

    class MyOnClickListner implements View.OnClickListener{
        private int clickPosition;
        public MyOnClickListner(int position){
            this.clickPosition=position;
        }
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(context,GameDetailActivity.class);
            intent.putExtra("game_id",gameListBean.getData().get(clickPosition).getId());
            intent.putExtra("title",gameListBean.getData().get(clickPosition).getTitle());
            context.startActivity(intent);
        }
    }
    public class ViewHolder
    {
        TextView btn_game;
        ImageView ivGameImg;
        TextView tvGameName;
        TextView tvAddress;
        TextView tvTime;
        TextView tvDistance;
    }
}
